package algotithm.acwing.rangeofproblems.test906;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/29-17:50
 * @Title 906. 区间分组
 */
import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(String.valueOf(reader.readLine()));

        ArrayList<int[]> list = new ArrayList<>();
        while (n-- > 0) {
            String[] num = reader.readLine().split(" ");
            int[] arr = new int[2];
            arr[0] = Integer.parseInt(num[0]);
            arr[1] = Integer.parseInt(num[1]);
            list.add(arr);
        }

        list.sort(new Comparator<int[]>(){
            @Override
            public int compare(int[] o1,int [] o2) {
                return o1[0] - o2[0];
            }
        });

        PriorityQueue<Integer> heap=new PriorityQueue<>();
        for(int i=0;i<list.size();i++){
            //堆是空或者堆的对顶是大于等于左端点 ,需要开一共新的组
            if (!heap.isEmpty() && heap.peek() < list.get(i)[0]) {
                heap.poll();
            }
            //说明当前区间可以放到某一组中，就把他放到最小值的组中
            heap.add(list.get(i)[1]);
        }
        writer.write(heap.size() + "\n");
        writer.flush();
        writer.close();
        reader.close();


    }

}
